/**
 * Copyright (c) 2009 Cerner Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cerner Corporation - initial API and implementation
 */
package com.cerner.system.rest.jaas.cas.functional;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;

/**
 * Stub implementation of the JAAS {@link CallbackHandler} interface that
 * handles {@link NameCallback} and {@link PasswordCallback} objects.
 * 
 * @author Alex Horn
 */
class StubCallbackHandler implements CallbackHandler {

  private String username;
  private char[] password;

  StubCallbackHandler(String username, char[] password) {
    this.username = username;
    this.password = password.clone();
  }

  public void handle(Callback[] callbacks) {
    for (Callback cb : callbacks) {
      if (cb instanceof NameCallback) {
        final NameCallback ncb = (NameCallback) cb;
        ncb.setName(this.username);
      } else if (cb instanceof PasswordCallback) {
        final PasswordCallback pcb = (PasswordCallback) cb;
        pcb.setPassword(this.password.clone());
      } else {
        throw new AssertionError(
            "Cannot handle ".concat(cb.getClass().getSimpleName()));
      }
    }

  }

}
